var XMLHttp = {
    _objPool: [],
    
    _getInstance: function ()
    {
        for (var i = 0; i < this._objPool.length; i ++)
        {
            if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4)
            {
                return this._objPool[i];
            }
        }
        
        // IE5中不支持push方法
        this._objPool[this._objPool.length] = this._createObj();

        return this._objPool[this._objPool.length - 1];
    },

    _createObj: function ()
    {
        if (window.XMLHttpRequest)
        {
            var objXMLHttp = new XMLHttpRequest();

        }
        else
        {
            var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
            for(var n = 0; n < MSXML.length; n ++)
            {
                try
                {
                    var objXMLHttp = new ActiveXObject(MSXML[n]);        
                    break;
                }
                catch(e)
                {
                }
            }
         }          
        
        // mozilla某些版本没有readyState属性
        if (objXMLHttp.readyState == null)
        {
            objXMLHttp.readyState = 0;

            objXMLHttp.addEventListener("load", function ()
                {
                    objXMLHttp.readyState = 4;
                    
                    if (typeof objXMLHttp.onreadystatechange == "function")
                    {
                        objXMLHttp.onreadystatechange();
                    }
                },  false);
        }

        return objXMLHttp;
    },   
    /*生产批号在线验证的onreadystate函数*/
check_p_online: function (method, url, data,img_id){
	      var objXMLHttp = this._getInstance();
        with(objXMLHttp)
        {
            try
            {
                // 加随机数防止缓存
                if (url.indexOf("?") > 0)
                {
                    url += "&randnum=" + Math.random();
                }
                else
                {
                    url += "?randnum=" + Math.random();
                }

                open(method, url, true);
                
                // 设定请求编码方式
                setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                	onreadystatechange = function(){ 
                		var img_obj = document.getElementById(img_id);
                		if (objXMLHttp.readyState == 4){
                			if(objXMLHttp.status == 200 || objXMLHttp.status == 304){                     
                    			if (objXMLHttp.responseText == 0){
       												img_obj.src="../images/sigh.gif";
       										}else if(objXMLHttp.responseText == -1){
       														img_obj.src="../images/check_error.gif";
       										}else{
       														img_obj.src="../images/check_right.gif";
       										}
                    	}else{
                    					img_obj.src="";
       												alert("服务器错误，请稍后再试！");
                    	}
                  	}
                  }
            }catch(e){
                alert(e);
            }
        }
      },
   /*问题产品输入批号在线验证*/   
check_pack_online: function (method, url, data,img_obj){
	      var objXMLHttp = this._getInstance();
        with(objXMLHttp)
        {
            try
            {
                // 加随机数防止缓存
                if (url.indexOf("?") > 0)
                {
                    url += "&randnum=" + Math.random();
                }
                else
                {
                    url += "?randnum=" + Math.random();
                }

                open(method, url, true);
                
                // 设定请求编码方式
                setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                	onreadystatechange = function(){ 
                		if (objXMLHttp.readyState == 4){
                			if(objXMLHttp.status == 200 || objXMLHttp.status == 304){                     
                    			if (objXMLHttp.responseText == 1){
       												img_obj.src="../images/check_right.gif";
       										}else{
       											if(objXMLHttp.responseText == 0){
       												img_obj.src="../images/sigh.gif";
       											}
       										}
                    	}else{
                    					img_obj.src="";
       												alert("服务器错误，请稍后再试！");
                    	}
                  	}
                  }
            }catch(e){
                alert(e);
            }
        }
      },
    /*生产批号在线修改的onreadystate函数*/
alertsend: function (method, url, data,img_obj,span_obj,node_obj,fvalue){
	      var objXMLHttp = this._getInstance();
        with(objXMLHttp)
        {
            try
            {
                // 加随机数防止缓存
                if (url.indexOf("?") > 0)
                {
                    url += "&randnum=" + Math.random();
                }
                else
                {
                    url += "?randnum=" + Math.random();
                }

                open(method, url, true);
                
                // 设定请求编码方式
                setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                onreadystatechange = function(){
                    if (objXMLHttp.readyState == 4){
                    	if(objXMLHttp.status == 200 || objXMLHttp.status == 304){
                    			if (objXMLHttp.responseText == 0)
       												img_obj.src="../images/check_error.gif";
       										else if(objXMLHttp.responseText == 1){
       												img_obj.style.display="none";
       												node_obj.style.display = "none";
       												span_obj.innerHTML = fvalue;
       												span_obj.style.display = "inline";
       										}else{
       														img_obj.src="../images/sigh.png";
       										}
                    		}else{
       														img_obj.src="";
       														img_obj.title="错误";
       														alert("服务器错误，请稍后重试！");
                    		}
                    	}
                	}
            }catch(e){
                alert(e);
            }
        }
       },
    /*生产批号在线删除的onreadystate函数*/
    deletesend: function (method, url, data,img_id,tr_obj,row){
    	  var objXMLHttp = this._getInstance();
        with(objXMLHttp)
        {
            try
            {
                // 加随机数防止缓存
                if (url.indexOf("?") > 0)
                {
                    url += "&randnum=" + Math.random();
                }
                else
                {
                    url += "?randnum=" + Math.random();
                }

                open(method, url, true);
                
                // 设定请求编码方式
                setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                onreadystatechange = function(){
                    if (objXMLHttp.readyState == 4){
                    	if(objXMLHttp.status == 200 || objXMLHttp.status == 304){
                    			img_obj = document.getElementById(img_id);
                    			if(objXMLHttp.responseText == 1){
       												img_obj.src="../images/undo.png";
       												img_obj.title="撤消";
       												img_obj.onclick=function (){dedo(row,1)};
															Tips.remove(img_id);
       												var td_arr=tr_obj.getElementsByTagName('td');
                    					for (var i = 1 ;i<=11;++i){
                    						td_arr[i].onclick=function(){};
                    					}
       										}
                    	}else{
       														img_obj.src="";
       														img_obj.title="错误";
       														alert("服务器错误，请稍后重试！");
                    }
                   }
                }
            }catch(e){
                alert(e);
            }
        }
                },
    /*生产批号在线撤消的onreadystate函数*/
    undosend: function (method, url, data,img_id,tr_obj,row){
    	  var objXMLHttp = this._getInstance();
        with(objXMLHttp)
        {
            try
            {
                // 加随机数防止缓存
                if (url.indexOf("?") > 0)
                {
                    url += "&randnum=" + Math.random();
                }
                else
                {
                    url += "?randnum=" + Math.random();
                }

                open(method, url, true);
                
                // 设定请求编码方式
                setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
                send(data);
                onreadystatechange = function(){
                    if (objXMLHttp.readyState == 4){
                    	if(objXMLHttp.status == 200 || objXMLHttp.status == 304){
                    			img_obj = document.getElementById(img_id);
                    			if(objXMLHttp.responseText == 1){
       												img_obj.src="../images/chk_off.png";
       												img_obj.title="删除";
       												img_obj.onclick=function (){dedo(row,0)};
       												Tips.remove(img_id);
       												var td_arr=tr_obj.getElementsByTagName('td');
                    					td_arr[1].onclick =function (){alter_state(row,'fdate','input',1,'product')};
                    					td_arr[2].onclick =function (){alter_state(row,'frog','input',1,'product')};
                    					td_arr[3].onclick =function (){alter_state(row,'fclient','input',1,'product')};	
                    					td_arr[4].onclick =function (){alter_state(row,'fspec','select',1,'product')};	
                    					td_arr[5].onclick =function (){alter_state(row,'fproduct','input',1,'product')};	
                    					td_arr[6].onclick =function (){alter_state(row,'fweight','input',1,'product')};	
                    					td_arr[7].onclick =function (){alter_state(row,'fmould','input',1,'product')};	
                    					td_arr[8].onclick =function (){alter_state(row,'fdefect','input',1,'product')};	
                    					td_arr[9].onclick =function (){alter_state(row,'fsurface','input',1,'product')};	
                    					td_arr[10].onclick =function (){alter_state(row,'fjudge','select',1,'product')};
                    					td_arr[11].onclick =function (){alter_state(row,'fremarks','input',1,'product')};		
       										}
                    	}else{
       														img_obj.src="";
       														img_obj.title="错误";
       														alert("服务器错误，请稍后重试！");
                    	}
                    }
               }
            }catch(e){
                alert(e);
            }
        }
                }
};

/* 静态生成一个XMLHttpRequest对象 */
		var request = false;
		try {
  	request = new XMLHttpRequest();
		} catch (trymicrosoft) {    
  			try {
    			request = new ActiveXObject("Msxml2.XMLHTTP");
  			} catch (othermicrosoft) {
    				try {
      				request = new ActiveXObject("Microsoft.XMLHTTP");// 新版的微软浏览器Msxml2.XMLHTTP
    				} catch (failed) {
      				request = false;
    				}
  			}		
		}
		if (!request)
  		alert("Error initializing XMLHttpRequest!");


var rog_re=/[y|t][a-z]\d{10}/i;//原材料正则
var supplier_re=/[a-zA-Z]/;//厂家代码正则
/*原材料查询记录中浮动框*/
function echoAnneal(i)
{
var lockDiv = document.getElementById("suoding");
var MsgWindow = document.getElementById("div" + i);
lockDiv.style.left=0;
lockDiv.style.top='0';
if (document.documentElement && document.documentElement.clientHeight)
    {
        var doc = document.documentElement;
				if(doc.clientHeight>doc.scrollHeight)
					lockDiv.style.height=doc.clientHeight + 'px';
				else {
					lockDiv.style.height=doc.scrollHeight +'px';
				}
        lockDiv.style.width = (doc.clientWidth>doc.scrollWidth)?doc.clientWidth-1 : doc.scrollWidth-2 + 'px';
    }
else
    {
        var doc = document.body;
        lockDiv.style.width = (window.innerWidth>doc.scrollWidth)?window.innerWidth:doc.scrollWidth + 'px';
        lockDiv.style.height = (window.innerHeight>doc.scrollHeight)?window.innerHeight:doc.scrollHeight + 'px';
    }
lockDiv.style.display="block";
MsgWindow.style.display="block";
}
/*关闭浮动框*/
function _close(k) 
{
var lockDiv = document.getElementById("suoding");
var MsgWindow = document.getElementById("div" + k);
lockDiv.style.display="none";
MsgWindow.style.display="none";
}

/* 验证原材料预输入 */
function check_preroginput(){
	if (!rog_re.test(document.getElementById("preraw").value)){
		alert("原料批号输入不正确!");
		document.getElementById("preraw").focus();
		return false;
	}
	 if(document.getElementById("prenum").value <=0  ||  document.getElementById("prenum").value != parseInt(document.getElementById("prenum").value)){
    alert("记录数输入不正确!");
		document.getElementById("prenum").focus();
		return false;
    }
}

/*修改密码验证*/

/* 可修改人员在线操作查询结果状态修改*/
function alter_state(row,whois,suffix,performance,item){//这里的参数row表示第几行、whois哪一个主体，比如fdate、suffix哪一个节点，比如select、performance表示是用户修改还是服务器更新，1为用户更改，0为更新、item表示是产品还是原料，这个是为原料作准备，product为产品，rog为原料
	var allname = whois + suffix +row;
	var allspan = whois + 'span' +row;
	var allimg = whois + 'img' +row;
	img_obj=document.getElementById(allimg);
	name_obj=document.getElementById(allname);
	span_obj=document.getElementById(allspan);
	if (performance){
		name_obj.style.display='inline';
		span_obj.style.display='none';
		name_obj.focus();
	}else{
		img_obj.style.display = 'inline';
		/*如果用户没有修改*/		
		if (span_obj.innerHTML==name_obj.value || (allimg== "fjudge" + "img" + row && name_obj.options[name_obj.selectedIndex].text== span_obj.innerHTML)){
			img_obj.style.display="none";
			span_obj.style.display="inline";
			name_obj.style.display="none";	
		}else{
			var fvalue;
			if(suffix=="select"){
				fvalue=name_obj.options[name_obj.selectedIndex].text;
			}else{
				fvalue=name_obj.value;
			}
			if(item == 'product'){
				product_id = "fproductspan" + row;
				var send_url = "check.php?action=pquery_edit&product=" + document.getElementById(product_id).innerHTML + "&item=" + whois + "&value=" + fvalue;
			}
			if(item == 'rog'){
					product_id = "frogspan" + row;
					var send_url = "check.php?action=rquery_edit&product=" + document.getElementById(product_id).innerHTML + "&item=" + whois + "&value=" + fvalue;
			}
				XMLHttp.alertsend('get',send_url,'',img_obj,span_obj,name_obj,fvalue);
		}
	}
}

/* 在线操作查询结果删除与恢复 参数1为行数，参数2表示删除或者撤消，1为撤消，0为删除*/
function dedo(row,bool){
	img_id="fdeleteimg"+row
	img_obj=document.getElementById(img_id);
	tr_obj=document.getElementById("row" + row);
	img_obj.src='../images/loading1.gif'
	img_obj.title="进行中...";
	img_obj.onclick=new function(){};
	var product = "fproductspan" +row;
	if(bool){
		var undo_rul = "check.php?action=pquery_undo&product=" + document.getElementById(product).innerHTML;
		XMLHttp.undosend('get',undo_rul,'',img_id,tr_obj,row);
	}else{
		var delete_url = "check.php?action=pquery_delete&product=" + document.getElementById(product).innerHTML;
		XMLHttp.deletesend('get',delete_url,'',img_id,tr_obj,row);
	}
}
/* 生产记录输入在线验证 */
function xml_check(id_pre,nodenum,remarks,product_rows){
	var element_id = id_pre + nodenum;
	var id_img = id_pre + "_img" + nodenum;
	element_obj = document.getElementById(element_id);//要验证的节点对象
	img_obj = document.getElementById(id_img);//要验证的节点后的X图像对象
	var judgeText=document.getElementById("ejudge" + nodenum).options[document.getElementById("ejudge" + nodenum).selectedIndex].text;//得到ejudge显示的值
	var inputValue=element_obj.value;
	var product_id="eproduct" + nodenum;
	var product_all=document.getElementById(product_id).value;
	if(product_all=="")//如果产品批号是空的，则不向下验证，直接返回空
		return true;
	if(remarks == "product"){//第三个参数为产品 使用在线验证
		if(inputValue !=""){
			img_obj.src="../images/loading.gif";
			if (!product_re.test(inputValue)){
				img_obj.src="../images/check_error.gif";
				return false;
			}
			var product_url = "check.php?action=product_edit&value=" + element_obj.value;
			XMLHttp.check_p_online('GET',product_url,'',id_img);		
		}else
			return true;
	}
	if (id_pre == "eremarks"){
		if ( judgeText != "合格" && inputValue == ""){
				img_obj.src="../images/check_error.gif";
				return false;
			}
		if (judgeText != "合格" && inputValue != ""){
				img_obj.src="../images/check_right.gif";
				return true;
			}
			return true;
		}
		
	if(remarks == "defect"){//第三个参数为伤点
		if(judgeText == "合格" || inputValue != "")
			if (!isint(inputValue)){
					
				img_obj.src="../images/check_error.gif";
				return false;
				}
		img_obj.src="../images/check_right.gif";
  	return true;
  	}
  	
  if(remarks == "mould"){//第三个参数为模具 
		if (inputValue !="" && !mould_re.test(inputValue)){
			img_obj.src="../images/check_error.gif";
			return false;
		}
		img_obj.src="../images/check_right.gif";
		return true;
	}
	
	if (inputValue==""){
		img_obj.src="../images/check_error.gif"; //上面验证的对象可以为空，下面验证对象为空,直接返回false
		return false;
	}
	if(!remarks){
		  img_obj.src="../images/check_right.gif";
			return true;  //对象不为空，第三个参数为空，返回true，这个是为不需要验证数据类型的，只要确定为空就可以的对象
	}
	if(remarks == "rog"){  //第三个参数为原材料
		if (!rog_re.test(inputValue)){
			img_obj.src="../images/check_error.gif";
			return false;
		}
		img_obj.src="../images/check_right.gif";
		return true;
	}
	if(remarks == "weight"){//第三个参数为重量
		if (!isfloat(inputValue)){
			img_obj.src="../images/check_error.gif";
			return false;
		}
		img_obj.src="../images/check_right.gif";
		return true;
	}
}
/* 点击检查按键时验证生产记录输入表单*/
function check_pedit(){
	var row_obj = document.getElementById("ptable");
	var rows = row_obj.rows.length-1;//加上表头，比子行多1，所以减1
	for (var i=1;i<=rows;++i){
			xml_check("eclient",i);
			xml_check("erog",i,"rog");
			xml_check("eweight",i,"weight");
			xml_check("emould",i,"mould");
			xml_check("edefect",i,"defect");
			xml_check("eremarks",i,"remarks");
		}
	for (var i=1;i<=rows;++i){
		xml_check("eproduct",i,"product");
	}
}
/* 点击检查按键时验证问题产品输入表单*/
function check_pack(){
	var row_obj = document.getElementById("ptbody");
	var rows = row_obj.rows.length;//共有几行
	var element_id,element_obj,id_img,img_obj;
	var tr_arr=row_obj.getElementsByTagName("tr");
	var input_arr=new Array();
	var img_arr=new Array();
	for (var i=0;i<rows;++i){
			input_arr=tr_arr[i].getElementsByTagName("input");
			img_arr=tr_arr[i].getElementsByTagName("img");
			img_arr[0].src="../images/loading.gif";
			img_arr[1].src="../images/loading.gif";
			if (input_arr[0].value !="" ){
				if(!product_re.test(input_arr[0].value ))
					img_arr[0].src="../images/check_error.gif";
				else{
					var product_url = "check.php?action=pack_edit&value=" + input_arr[0].value;
					XMLHttp.check_pack_online('GET',product_url,'',img_arr[0]);
				}
				if(input_arr[1].value !=""){
					img_arr[1].src="../images/check_right.gif";
				}else{
					img_arr[1].src="../images/check_error.gif";
				}				
			}else{
				img_arr[0].src="../images/check_right.gif";
				img_arr[1].src="../images/check_right.gif";
			}
			
	}
}
/*点击添加按钮时增加行*/
function addrow(){
		var row_obj = document.getElementById("ptbody");
		var rows = row_obj.rows.length+1;//共几行+1
		var newrow=row_obj.insertRow(-1);
		newrow.innerHTML='<td><select style="text-align:center;width:70px;" name="epack' + rows + '"><option value="包装">包装</option><option value="销售">销售</option></select></td><td><input name="eproduct' + rows + '" type="text" style="text-align:center;width:100px" maxlength="12" size="12"/><img src="../images/check_blank.gif" align="absmiddle" onmouseover="null" /></td><td><select style="text-align:center;width:70px;" name="ejudge' + rows + '"><option value="合格">合格</option><option value="不合格">不合格</option><option value="待检">待检</option></select></td><td><input name="eremarks' + rows + '" type="text" style="text-align:center;" maxlength="40" size="10"/><img src="../images/check_blank.gif" align="absmiddle"/></td><td valign="center"><button type="button" class="positive" style="margin-top:0;padding:3px 4px 0 4px" onclick="addrow()">添加</button></td>';
}
/*点击导出结果按钮时post生产批号给服务端*/
function outputSend(){
	var row_obj = document.getElementById("rresult");
	var rows = row_obj.rows.length;
	var randomout=PostData;//把随机数赋给变量传至服务器验证表格是否生成完成
	var PostData = document.getElementById("raw1").innerHTML;
	for (var i=2;i<=rows;++i){
		PostData += '|' + document.getElementById("raw" + i).innerHTML;
	}
	document.getElementById("outmessage").value=PostData;
	document.getElementById("outform").submit();
	document.getElementById('message').innerHTML='正在生成...'
	document.getElementById("outputButton").disabled=1;
	window.setTimeout("outputrespone()",1000);
}
function outputrespone(){
	request.open('POST','check.php',true);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	request.onreadystatechange=function(){
			if (request.readyState == 4){
				if (request.status == 200 || request.status == 304){ 
					if(request.responseText==1){
						document.getElementById('message').innerHTML='完成，请使用浏览器直接下载！'
						document.getElementById('outputButton').disabled=0;
					}else{
						window.setTimeout("outputrespone()",1000);
					}
				}else{
					document.getElementById('message').innerHTML='服务器错误，请稍候再试'
					document.getElementById('outputButton').disabled=0;
				}
			}
		};
	var checkoutput='action=output';
	request.send(checkoutput);
	}

//生产记录查询验证表单
function check_rogform(){
	var str1=document.getElementById("rbegin").value.replace(/-/g,'/');
	var str2=document.getElementById("rend").value.replace(/-/g,'/');
	if (document.getElementById("rbegin").value == "") {
		alert("起始日期不能为空！");
		document.getElementById("rbegin").focus();
		return false;
	}
	if (document.getElementById("rend").value == "") {
		alert("终止日期不能为空！");
		document.getElementById("rend").focus();
		return false;
	}	
	var d1 = new Date(str1);
	var d2 = new Date(str2);
	if (Date.parse(d1) - Date.parse(d2) > 0) {
		alert("终止日期不能小于初始日期，请重新输入");
		return false;
	}
	
	if (document.getElementById("rsupplier").value !='' && !supplier_re.test(document.getElementById("rsupplier").value)){
		alert("投料批号输入不正确");
		document.getElementById("rsupplier").focus();
		return false;
	}
}
/* 验证是否数字 */
function isfloat(r){  
            if( isNaN(parseFloat(r)) || parseFloat(r)<0 || parseFloat(r) != r )
            {
                return false;
            }
         return true;  
}
/* 验证是否正整数 */
function isint(r){  
            if( isNaN(parseInt(r)) || parseInt(r)<0 || parseInt(r) != r )
            {
                return false;
            }
         return true;  
}